﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;

    public class NannySpecification : ISpecification<Nanny, NannySearch>
    {
        public NannySpecification()
        {
            this.SearchParams = new NannySearch();
        }

        public NannySearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<Nanny, bool>> Expression
        {
            get 
            {
                IQueryBuilder<Nanny> queryBuilder = QueryBuilder.Create<Nanny>();

                if (null != SearchParams)
                {
                    if (SearchParams.CorporationID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(n => n.Corporation.ID, SearchParams.CorporationID);
                    }
                    if (null != SearchParams.Type)
                    {
                        queryBuilder = queryBuilder.Equals(n => (int)n.Type, SearchParams.Type.GetHashCode());
                    }
                    if (SearchParams.Experience > 0)
                    {
                        queryBuilder = queryBuilder.Equals(n => n.Experience, SearchParams.Experience);
                    }
                    if (SearchParams.Age > 0)
                    {
                        queryBuilder = queryBuilder.Equals(n => n.Age, SearchParams.Age);
                    }
                    if (SearchParams.IsMarried.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(n => n.IsMarried, SearchParams.IsMarried.Value);
                    }
                    if (SearchParams.Degree > 0)
                    {
                        queryBuilder = queryBuilder.Equals(n => n.Degree, SearchParams.Degree);
                    }
                    if (null != SearchParams.SkillPosition)
                    {
                        foreach (int sp in SearchParams.SkillPosition)
                        {
                            queryBuilder = queryBuilder.Greater(n => n.MainSkill & (long)Math.Pow(2, sp), 0);
                        }
                    }
                    if (SearchParams.IsRecommend)
                    {
                        queryBuilder = queryBuilder.Greater(n => n.NannyStatus.RecommendTimeout, DateTime.Now);
                    }
                    if (SearchParams.IsSystemRecommend)
                    {
                        queryBuilder = queryBuilder.Greater(n => n.NannyStatus.SystemRecommendTimeout, DateTime.Now);
                    }
                    if (SearchParams.MinScore > 0 && SearchParams.MaxScore > 0)
                    {
                        queryBuilder = queryBuilder.Greater(n => n.UserAccount.Score, SearchParams.MinScore);
                        queryBuilder = queryBuilder.LessThanEqual(n => n.UserAccount.Score, SearchParams.MaxScore);
                    }
                    if (SearchParams.OnLineStatus.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(n => (int)n.NannyStatus.OnLineStatus, SearchParams.OnLineStatus.Value.GetHashCode());
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
